/**
 * API错误码常量
 */

/**
 * 错误码枚举
 */
export enum ErrorCode {
  // 通用状态码 (2xx 成功)
  SUCCESS = 200,
  CREATED = 201,
  ACCEPTED = 202,
  NO_CONTENT = 204,

  // 客户端错误 (4xx)
  BAD_REQUEST = 400,
  UNAUTHORIZED = 401,
  FORBIDDEN = 403,
  NOT_FOUND = 404,
  METHOD_NOT_ALLOWED = 405,
  CONFLICT = 409,
  VALIDATION_FAILED = 422,
  TOO_MANY_REQUESTS = 429,

  // 服务器错误 (5xx)
  INTERNAL_SERVER_ERROR = 500,
  SERVICE_UNAVAILABLE = 503,
  GATEWAY_TIMEOUT = 504,

  // 业务错误码 (1000-1999)
  BUSINESS_ERROR = 1000,

  // 用户管理错误码 (1001-1099)
  USER_NOT_FOUND = 1001,
  USERNAME_ALREADY_EXISTS = 1002,
  PASSWORD_INCORRECT = 1003,
  USER_DISABLED = 1004,
  USER_LOCKED = 1005,
  INVALID_TOKEN = 1006,
  TOKEN_EXPIRED = 1007,
  TOKEN_REFRESH_FAILED = 1008,

  // 权限管理错误码 (1100-1199)
  PERMISSION_DENIED = 1100,
  ROLE_NOT_FOUND = 1101,
  ASSIGN_PERMISSION_FAILED = 1102,
  REMOVE_PERMISSION_FAILED = 1103,
  PRODUCT_ACCESS_DENIED = 1104,

  // 产品管理错误码 (1200-1299)
  PRODUCT_NOT_FOUND = 1200,
  PRODUCT_CODE_ALREADY_EXISTS = 1201,
  PRODUCT_DELETE_FAILED = 1202,
  PRODUCT_UPDATE_FORBIDDEN = 1203,

  // 元数据管理错误码 (1300-1399)
  SCHEMA_NOT_FOUND = 1300,
  SCHEMA_ALREADY_EXISTS = 1301,
  TABLE_NOT_FOUND = 1302,
  TABLE_ALREADY_EXISTS = 1303,
  COLUMN_NOT_FOUND = 1304,
  COLUMN_ALREADY_EXISTS = 1305,
  CONSTRAINT_NOT_FOUND = 1306,
  CONSTRAINT_ALREADY_EXISTS = 1307,
  INDEX_NOT_FOUND = 1308,
  INDEX_ALREADY_EXISTS = 1309,

  // 字段库错误码 (1400-1499)
  FIELD_LIBRARY_NOT_FOUND = 1400,
  FIELD_ALREADY_EXISTS = 1401,
  FIELD_MAPPING_NOT_FOUND = 1402,
  FIELD_MAPPING_ALREADY_EXISTS = 1403,
  FIELD_TYPE_NOT_SUPPORTED = 1404,
  FIELD_VALIDATION_FAILED = 1405,

  // 数据字典错误码 (1500-1599)
  DICTIONARY_NOT_FOUND = 1500,
  DICTIONARY_CODE_ALREADY_EXISTS = 1501,
  DICTIONARY_ITEM_NOT_FOUND = 1502,
  DICTIONARY_ITEM_CODE_ALREADY_EXISTS = 1503,

  // DDL生成错误码 (1600-1699)
  DDL_GENERATION_FAILED = 1600,
  UNSUPPORTED_DATABASE_TYPE = 1601,
  DDL_TEMPLATE_NOT_FOUND = 1602,

  // 文件操作错误码 (1700-1799)
  FILE_UPLOAD_FAILED = 1700,
  FILE_DOWNLOAD_FAILED = 1701,
  FILE_FORMAT_NOT_SUPPORTED = 1702,
  FILE_SIZE_EXCEEDED = 1703,

  // 系统错误码 (9000-9999)
  SYSTEM_BUSY = 9000,
  DATABASE_ERROR = 9001,
  NETWORK_ERROR = 9002,
  CONFIGURATION_ERROR = 9003,
  DEPENDENCY_SERVICE_ERROR = 9004,
}

/**
 * 错误码消息映射
 */
export const ERROR_MESSAGES: Record<ErrorCode, string> = {
  [ErrorCode.SUCCESS]: '操作成功',
  [ErrorCode.CREATED]: '创建成功',
  [ErrorCode.ACCEPTED]: '请求已接受，正在处理中',
  [ErrorCode.NO_CONTENT]: '删除成功',

  [ErrorCode.BAD_REQUEST]: '请求参数错误',
  [ErrorCode.UNAUTHORIZED]: '未授权，请登录',
  [ErrorCode.FORBIDDEN]: '权限不足，拒绝访问',
  [ErrorCode.NOT_FOUND]: '请求的资源不存在',
  [ErrorCode.METHOD_NOT_ALLOWED]: '请求方法不被允许',
  [ErrorCode.CONFLICT]: '资源冲突',
  [ErrorCode.VALIDATION_FAILED]: '数据验证失败',
  [ErrorCode.TOO_MANY_REQUESTS]: '请求过于频繁，请稍后重试',

  [ErrorCode.INTERNAL_SERVER_ERROR]: '服务器内部错误',
  [ErrorCode.SERVICE_UNAVAILABLE]: '服务暂不可用',
  [ErrorCode.GATEWAY_TIMEOUT]: '网关超时',

  [ErrorCode.BUSINESS_ERROR]: '业务处理失败',

  [ErrorCode.USER_NOT_FOUND]: '用户不存在',
  [ErrorCode.USERNAME_ALREADY_EXISTS]: '用户名已存在',
  [ErrorCode.PASSWORD_INCORRECT]: '密码错误',
  [ErrorCode.USER_DISABLED]: '用户已被禁用',
  [ErrorCode.USER_LOCKED]: '用户已被锁定',
  [ErrorCode.INVALID_TOKEN]: '无效的令牌',
  [ErrorCode.TOKEN_EXPIRED]: '令牌已过期',
  [ErrorCode.TOKEN_REFRESH_FAILED]: '令牌刷新失败',

  [ErrorCode.PERMISSION_DENIED]: '权限不足',
  [ErrorCode.ROLE_NOT_FOUND]: '角色不存在',
  [ErrorCode.ASSIGN_PERMISSION_FAILED]: '权限分配失败',
  [ErrorCode.REMOVE_PERMISSION_FAILED]: '权限移除失败',
  [ErrorCode.PRODUCT_ACCESS_DENIED]: '无权限访问该产品',

  [ErrorCode.PRODUCT_NOT_FOUND]: '产品不存在',
  [ErrorCode.PRODUCT_CODE_ALREADY_EXISTS]: '产品代号已存在',
  [ErrorCode.PRODUCT_DELETE_FAILED]: '产品删除失败（存在关联数据）',
  [ErrorCode.PRODUCT_UPDATE_FORBIDDEN]: '无权限修改该产品',

  [ErrorCode.SCHEMA_NOT_FOUND]: '数据库模式不存在',
  [ErrorCode.SCHEMA_ALREADY_EXISTS]: '数据库模式已存在',
  [ErrorCode.TABLE_NOT_FOUND]: '数据表不存在',
  [ErrorCode.TABLE_ALREADY_EXISTS]: '数据表已存在',
  [ErrorCode.COLUMN_NOT_FOUND]: '字段不存在',
  [ErrorCode.COLUMN_ALREADY_EXISTS]: '字段已存在',
  [ErrorCode.CONSTRAINT_NOT_FOUND]: '约束不存在',
  [ErrorCode.CONSTRAINT_ALREADY_EXISTS]: '约束已存在',
  [ErrorCode.INDEX_NOT_FOUND]: '索引不存在',
  [ErrorCode.INDEX_ALREADY_EXISTS]: '索引已存在',

  [ErrorCode.FIELD_LIBRARY_NOT_FOUND]: '字段库记录不存在',
  [ErrorCode.FIELD_ALREADY_EXISTS]: '字段已存在',
  [ErrorCode.FIELD_MAPPING_NOT_FOUND]: '字段映射不存在',
  [ErrorCode.FIELD_MAPPING_ALREADY_EXISTS]: '字段映射已存在',
  [ErrorCode.FIELD_TYPE_NOT_SUPPORTED]: '不支持的字段类型',
  [ErrorCode.FIELD_VALIDATION_FAILED]: '字段验证失败',

  [ErrorCode.DICTIONARY_NOT_FOUND]: '数据字典不存在',
  [ErrorCode.DICTIONARY_CODE_ALREADY_EXISTS]: '字典编号已存在',
  [ErrorCode.DICTIONARY_ITEM_NOT_FOUND]: '字典项不存在',
  [ErrorCode.DICTIONARY_ITEM_CODE_ALREADY_EXISTS]: '字典项编号已存在',

  [ErrorCode.DDL_GENERATION_FAILED]: 'DDL脚本生成失败',
  [ErrorCode.UNSUPPORTED_DATABASE_TYPE]: '不支持的数据库类型',
  [ErrorCode.DDL_TEMPLATE_NOT_FOUND]: 'DDL模板不存在',

  [ErrorCode.FILE_UPLOAD_FAILED]: '文件上传失败',
  [ErrorCode.FILE_DOWNLOAD_FAILED]: '文件下载失败',
  [ErrorCode.FILE_FORMAT_NOT_SUPPORTED]: '不支持的文件格式',
  [ErrorCode.FILE_SIZE_EXCEEDED]: '文件大小超出限制',

  [ErrorCode.SYSTEM_BUSY]: '系统繁忙，请稍后重试',
  [ErrorCode.DATABASE_ERROR]: '数据库操作失败',
  [ErrorCode.NETWORK_ERROR]: '网络连接异常',
  [ErrorCode.CONFIGURATION_ERROR]: '系统配置错误',
  [ErrorCode.DEPENDENCY_SERVICE_ERROR]: '依赖服务异常',
}

/**
 * 获取错误消息
 */
export function getErrorMessage(code: ErrorCode | number): string {
  return ERROR_MESSAGES[code as ErrorCode] || ERROR_MESSAGES[ErrorCode.INTERNAL_SERVER_ERROR]
}

/**
 * 判断是否为成功响应码
 */
export function isSuccessCode(code: number): boolean {
  return code >= 200 && code < 300
}

/**
 * 判断是否为客户端错误
 */
export function isClientErrorCode(code: number): boolean {
  return code >= 400 && code < 500
}

/**
 * 判断是否为服务器错误
 */
export function isServerErrorCode(code: number): boolean {
  return code >= 500 && code < 600
}

/**
 * 判断是否为业务错误
 */
export function isBusinessErrorCode(code: number): boolean {
  return code >= 1000 && code < 2000
}

/**
 * 判断是否为认证相关错误
 */
export function isAuthErrorCode(code: number): boolean {
  return code === ErrorCode.UNAUTHORIZED ||
         code === ErrorCode.INVALID_TOKEN ||
         code === ErrorCode.TOKEN_EXPIRED ||
         code === ErrorCode.TOKEN_REFRESH_FAILED
}

/**
 * 判断是否为权限相关错误
 */
export function isPermissionErrorCode(code: number): boolean {
  return code === ErrorCode.FORBIDDEN ||
         code === ErrorCode.PERMISSION_DENIED ||
         code === ErrorCode.PRODUCT_ACCESS_DENIED ||
         code === ErrorCode.PRODUCT_UPDATE_FORBIDDEN
}